package com.netty.cn.model.udp.server;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;

/**
 * 
 * @author wangzonghui
 * @date 2021-11-02 16:31:20
 * @Description udp客户端代码
 */
public class UDPClient {
	
	public static void main(String[] args) {
		Bootstrap bootstrap = new Bootstrap();
		EventLoopGroup workGroup = new NioEventLoopGroup();
		bootstrap.group(workGroup).channel(NioDatagramChannel.class)
		.option(ChannelOption.SO_BROADCAST, true)
		.handler(new ChannelInitializer<NioDatagramChannel>() {
 
			@Override
			protected void initChannel(NioDatagramChannel ch) throws Exception {
				ch.pipeline().addLast(new UDPClientHandler());
			}
		});
		try {
			Channel channel = bootstrap.bind(0).sync().channel();
			channel.closeFuture().sync().await();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
